package com.example.bluetooth;

import java.util.ArrayList;
import java.util.Set;

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class ActivityBluetooth extends Activity 
{
	private ArrayAdapter<String> mNewDevicesArrayAdapter;
	private ArrayList<String> laLista = new ArrayList<String>();
  TextView out;

  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    out = (TextView) findViewById(R.id.out);

    // Getting the Bluetooth adapter
    BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
    out.append("\nAdapter: " + adapter);

    // Check for Bluetooth support in the first place 
    // Emulator doesn't support Bluetooth and will return null
    if(adapter==null) { 
      out.append("\nBluetooth NOT supported. Aborting.");
      return;
    }
    
    // Starting the device discovery
    out.append("\nStarting discovery...");
    adapter.startDiscovery();
 // Register for broadcasts when a device is discovered
    IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
    this.registerReceiver(mReceiver, filter);

    // Register for broadcasts when discovery has finished
    filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
    this.registerReceiver(mReceiver, filter);
//    while(adapter.isDiscovering())
//    {
//    	
//    }
    out.append("\nDone with discovery...");

    // Listing paired devices
    out.append("\nDevices Pared:");
    Set<BluetoothDevice> devices = adapter.getBondedDevices();
    for (BluetoothDevice device : devices) {
      out.append("\nFound device: " + device);
    }
  }
  
  
//The BroadcastReceiver that listens for discovered devices and
  // changes the title when discovery is finished
  private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
      @Override
      public void onReceive(Context context, Intent intent) {
          String action = intent.getAction();

          // When discovery finds a device
          if (BluetoothDevice.ACTION_FOUND.equals(action)) {
              // Get the BluetoothDevice object from the Intent
              BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
              // If it's already paired, skip it, because it's been listed already
              if (device.getBondState() != BluetoothDevice.BOND_BONDED) {
//                  mNewDevicesArrayAdapter.add(device.getName() + "\n" + device.getAddress());
            	  laLista.add(device.getName());
            	  Log.d("bluetooth", "en el if dio "+ device.getName());
            	  
              }
          // When discovery is finished, change the Activity title
          } else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) 
          {
        	  Log.d("bluetooth", ""+laLista.size());
//              setProgressBarIndeterminateVisibility(false);
//              setTitle(R.string.select_device);
//              if (mNewDevicesArrayAdapter.getCount() == 0) {
//                  String noDevices = getResources().getText(R.string.none_found).toString();
//                  mNewDevicesArrayAdapter.add(noDevices);
//              }
          }
      }
  };

}